package com.gjs.freechat.web.init;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationObjectSupport;

import com.gjs.freechat.server.component.ChatSessionManage;
import com.gjs.freechat.server.component.UserManage;
import com.gjs.freechat.server.component.UserSessionPool;
import com.gjs.freechat.utils.FileUtils;
import com.gjs.freechat.web.utils.MyWebUtils;

@Service
public class WebApplicationInit extends WebApplicationObjectSupport {
	
	private static Logger logger = Logger.getLogger(WebApplicationInit.class);
	
	@Autowired
	private UserManage userManage;
	
	@Autowired
	private UserSessionPool userSessionPool;
	
	@Autowired
	private ChatSessionManage chatSessionManage;
	
	@Override
	protected void initServletContext(ServletContext servletContext) {
		super.initServletContext(servletContext);
		MyWebUtils.setServletContextPath(servletContext);
		MyWebUtils.setServletContextAttr(servletContext, "tags", userManage.getAllInterestTags());
		MyWebUtils.setServletContextAttr(servletContext, "allAvatarColors", userManage.getAllAvatarColors());
		MyWebUtils.setServletContextAttr(servletContext, "allAvatarIcons", userManage.getAllAvatarIcons());
		userSessionPool.addSessions(chatSessionManage.getAllChatSessions());
		
		FileUtils.createDirectorys(ChatSessionManage.IMAGE_STORE_LOCATION);
		
		logger.info("初始化完成");
	}
}
